﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Timers;
using System.Collections.ObjectModel;
using NetworkEasyShare;

namespace NetworkEasyShare 
{
    class broadcastMessage 
    {
        private Socket socket;
        private IPAddress ipAddr;
        private IPEndPoint ipEnd;

        private UdpClient udp;
        private int broadcastPort;
        private IPEndPoint endPoint;
        private Timer msgTimer;
        
        public trayIcon tIcon;
        public infoMessage incomingMessage;
        private ObservableCollection<search.ComputerData> mComputerCollection;

        public broadcastMessage(bool sendMode, ObservableCollection<search.ComputerData> ComputerCollection)
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            ipAddr = IPAddress.Parse("255.255.255.255");

            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                    new MulticastOption(ipAddr));

            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 2);


            IPEndPoint ipEnd = new IPEndPoint(ipAddr, 15000);
            
            //udp = new UdpClient();
            //broadcastPort = 15000;
            //endPoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), broadcastPort);

            //mComputerCollection = ComputerCollection;

            //msgTimer = new Timer();

            //if (sendMode)
            //    msgTimer.Elapsed += new ElapsedEventHandler(sendInfo);
            //else
            //    msgTimer.Elapsed += new ElapsedEventHandler(receiveInfo);

            //// Set the Interval to 5 seconds.
            //msgTimer.Interval = 5000;
            //msgTimer.Enabled = true;



        }

        public void sendInfo()
        {
            infoMessage msg = new infoMessage("neo", "online", "127.0.0.1", "00:11:22:33:44:55");
            sendMessage(msg.ToXml());
        }

        private void sendInfo(object source, ElapsedEventArgs e)
        {
            infoMessage msg = new infoMessage("neo", "online", "127.0.0.1", "00:11:22:33:44:55");
            sendMessage(msg.ToXml());
        }

        public infoMessage receiveInfo()
        {
            infoMessage msg = new infoMessage();
            msg.fromXml(msg.string2Stream(receiveMessage()));

            return msg;
        }

        public void receiveInfo(object source, ElapsedEventArgs e)
        {
            incomingMessage = null;
            incomingMessage = new infoMessage();
            incomingMessage.fromXml(incomingMessage.string2Stream(receiveMessage()));

            incomingMessage.IPaddr

            mComputerCollection.Add(new ComputerData
            {
                Nickname = "A New Game",
                IPAddr = "A New Creator",
                MACAddr = "A New Publisher"
            });

        }

        public void sendMessage(string str) {
            byte[] sendBytes = Encoding.ASCII.GetBytes(str);
            udp.Send(sendBytes, sendBytes.Length, endPoint);
        }

        public string receiveMessage() {
            byte[] receiveBytes = udp.Receive(ref endPoint);
            return Encoding.ASCII.GetString(receiveBytes);
        }
    }
}
